package cn.hfatec.shms.system.module.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.HashMap;


import cn.hfatec.shms.constants.ShmsConstant;
import cn.hfatec.shms.enums.KeyTypeEnum;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.authData.dao.AuthDataDao;
import cn.hfatec.shms.system.authData.model.AuthData;
import cn.hfatec.shms.system.authData.model.AuthDataParams;
import cn.hfatec.shms.system.module.model.ModuleView;
import cn.hfatec.shms.system.moduleAuth.dao.ModuleAuthDao;
import cn.hfatec.shms.utils.KeyGenUtils;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.system.module.model.Module;
import cn.hfatec.shms.system.module.dao.ModuleDao;
import cn.hfatec.shms.system.module.model.ModuleParams;

import javax.annotation.Resource;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("ModuleService")
public class ModuleService {

    public static final String BEAN_ID = "ModuleService";

    //注入Dao层
    @Resource(name = "ModuleDao")
    private ModuleDao moduleDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    @Resource(name = "ModuleAuthDao")
    private ModuleAuthDao moduleAuthDao;

    @Resource(name = "AuthDataDao")
    private AuthDataDao authDataDao;

    /**
     * 查询数据
     *
     * @param moduleParams
     */
    @Transactional
    public List queryModuleTable(ModuleParams moduleParams) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", moduleParams);
        return baseDao.queryList("queryModule", params);
    }


    /**
     * 插入数据
     *
     * @param module
     */
    @Transactional
    public void saveModule(Module module) throws KeyGenException {
        if (module.getId() != null) {
            Module result = getModule(module.getId());
            if (result != null) {
                moduleDao.update(module);
            } else {
                module.setModuleId(KeyGenUtils.createKeyByType(KeyTypeEnum.MODULE_KEY));
                moduleDao.insert(module);
            }
        } else {
            module.setModuleId(KeyGenUtils.createKeyByType(KeyTypeEnum.MODULE_KEY));
            moduleDao.insert(module);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     * @param moduleId
     */
    @Transactional
    public void deleteModule(Integer id, String moduleId) {
        moduleAuthDao.deleteModuleAuthForModuleId(moduleId);
        moduleDao.delete(id);
    }


    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    public Module getModule(Integer id) {

        return moduleDao.getModuleById(id);
    }

    /**
     * 查询所有的模块
     *
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Transactional
    public List<ModuleView> queryAllModuleList() throws InvocationTargetException, IllegalAccessException {
        //查询所有模块信息
        ModuleParams moduleParams = new ModuleParams();
        List<Module> allModuleList = moduleDao.queryModule(moduleParams);
        return getModuleList(ShmsConstant.ROOT_MODULE_ID, allModuleList);
    }

    /**
     * 根据模块ID获取数据
     * @param moduleId
     * @return
     */
    @Transactional
    public Module queryModuleByModuleId(String moduleId) {
        return moduleDao.queryModuleByModuleId(moduleId);
    }

    /**
     * 根据父节点ID、总模块数据，递归获取模块视图信息
     *
     * @param parentModuleId
     * @param allModuleList
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Transactional
    public List<ModuleView> getModuleList(String parentModuleId, List<Module> allModuleList) throws InvocationTargetException, IllegalAccessException {
        List<ModuleView> list = Lists.newArrayList();
        for (Module module : allModuleList) {
            if (module.getParentModuleId() != null && module.getParentModuleId().equals(parentModuleId)) {
                ModuleView moduleView = new ModuleView();
                BeanUtils.copyProperties(moduleView, module);
                moduleView.setChildrenModuleList(getModuleList(moduleView.getModuleId(), allModuleList));
                list.add(moduleView);
            }
        }
        return list;
    }

    /**
     * 根据父节点ID、总模块数据，递归获取模块与分配权限表格数据
     *
     * @param parentModuleId
     * @param allModuleList
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Transactional
    public List<ModuleView> getModuleViewList(String parentModuleId, List<Module> allModuleList) throws InvocationTargetException, IllegalAccessException {
        List<ModuleView> list = Lists.newArrayList();
        for (Module module : allModuleList) {
            if (module.getParentModuleId() != null && module.getParentModuleId().equals(parentModuleId)) {
                ModuleView moduleView = new ModuleView();
                BeanUtils.copyProperties(moduleView, module);
                moduleView.setChildrenModuleList(getModuleViewList(moduleView.getModuleId(), allModuleList));
                List<AuthData> authDataList = authDataDao.queryAuthDataByModuleId(module.getModuleId());
                moduleView.setAuthDataList(authDataList);
                list.add(moduleView);
            }
        }
        return list;
    }
}